package com.huangguolin.cloud.recycle.service.impl;

import com.obs.services.IObsClient;
import com.obs.services.ObsClient;
import com.obs.services.model.ObjectListing;
import com.obs.services.model.ObsObject;
import com.huangguolin.cloud.file.entity.SearchFileEntity;
import com.huangguolin.cloud.file.repository.FileDao;
//import com.huangguolin.cloud.login.entity.LoginUserEntity;
import com.huangguolin.cloud.mybatis.entity.PtFileEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.recycle.repository.RecycleDao;
import com.huangguolin.cloud.recycle.service.RecycleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RecycleServiceImpl implements RecycleService {
    @Value("${endpoint}")
    private String endpoint;
    @Value("${access.key.id}")
    private String accessKeyId;
    @Value("${secret.access.key}")
    private String secretAccessKey;
    @Value("${bucket.name}")
    private String bucketName;
    @Resource
    private RecycleDao recycleDao;

    /**
     * 判断是否为目录
     *
     * @return
     */
    public boolean isDir(String obsKey) {
        int index = obsKey.lastIndexOf("/");
        System.out.println(index);
        System.out.println(obsKey.length());
        if (index == obsKey.length() - 1) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 获取文件名称
     *
     * @param obsKey
     * @return
     */
    public String getFileName(String obsKey) {
        String fileName = obsKey;
        if (isDir(obsKey)) {
            fileName = obsKey.substring(0, obsKey.length() - 1);
        }
        int index = fileName.lastIndexOf("/");
        fileName = fileName.substring(index + 1);
        return fileName;
    }

    @Override
    public PadingRstEntity<PtFileEntity> queryFileListByPage(SearchFileEntity entity, PagingEntity paging) {
        PadingRstEntity<PtFileEntity> ptFileEntityPadingRstEntity = new PadingRstEntity<>();
        entity.deal();
        paging.dealColums(PtFileEntity.class);
        ptFileEntityPadingRstEntity.setPage(paging.getPage());
        List<PtFileEntity> list = recycleDao.queryFileListByPage(entity,paging);
        Integer total = RecycleDao.queryFileTotal(entity);
        ptFileEntityPadingRstEntity.setRawRecords(list);
        ptFileEntityPadingRstEntity.setTotal(total);
        ptFileEntityPadingRstEntity.putItem();
        return ptFileEntityPadingRstEntity;
    }

    @Override
    public void deleteFile(String fileUuid) {
        recycleDao.deleteFile(fileUuid);

    }

    @Override
    public void restoreFile(String fileUuid) {

        recycleDao.restoreFile(fileUuid);
    }
}